<?php
namespace app\api\controller\zfshop;
class Uservipnumslog extends Base
{

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    public function lists(){
        $d = $this->post();
        $map = [];
        if($d["type"]!="")$map[] = ["and","a.type","=",$d["type"]];
        $map[] = ["and","a.user_id","=",$this->userinfo["id"]];
        //找本月
        $month_time = month_time($d['ym']);
        $start_time = $month_time['start_time'];
        $end_time   = $month_time['end_time'];
        $ym = date("Ym",$start_time);

        $map[] = ["and","a.create_time",">=",$start_time];
        $map[] = ["and","a.create_time","<=",$end_time];
        $w = where($map);
        $order = "order by a.id desc";
        $sql = "
            select
                c.pic,c.username,c.phone,a.nums,a.create_time,a.type
            from
                user_vip_nums_log a
            inner join
                user_upgrade b on b.id = a.order_upgrade_id
            inner join
                user c on c.id = b.user_id
        $w[0] {$order}";
        $list = $this->db->query($sql,$w[1]);
        if(!$list){
            //如果没有找到
            //找 <$end_time 的第一条
            $map = [];
            if($d["type"]!="")$map[] = ["and","type","=",$d["type"]];
            $map[] = ["and","user_id","=",$this->userinfo["id"]];
            $map[] = ["and","create_time","<",$start_time];
            $w = where($map);
            $sql = "select * from user_vip_nums_log $w[0] order by id desc";
            $rs = $this->db->query($sql,$w[1],1);
            if($rs){
                $map = [];
                if($d["type"]!="")$map[] = ["and","a.type","=",$d["type"]];
                $map[] = ["and","a.user_id","=",$this->userinfo["id"]];
                $w = where($map);
                $ym = date("Ym",$rs['create_time']);
                $start_time = strtotime($ym."01 00:00:00");
                $end_time = strtotime(date("Ymt 23:59:59",$start_time));
                $order = "order by a.id desc";
                $sql = "
                    select
                        c.pic,c.username,c.phone,a.nums,a.create_time,a.type
                    from
                        user_vip_nums_log a
                    inner join
                        user_upgrade b on b.id = a.order_upgrade_id
                    inner join
                        user c on c.id = b.user_id
                $w[0]  and a.create_time >= {$start_time} and a.create_time <= {$end_time} {$order}";
                $list = $this->db->query($sql,$w[1]);
            }
        }
        foreach($list as $k => $v){
            $list[$k]['create_time_name'] = com_time_format($v['create_time']);
        }
        suc(["list"=>$list,'curl_ym'=>$ym,'ym'=>date("Y-m",strtotime('-1 month',strtotime(date('Ym01',$start_time))))]);
    }

}
